import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { RouterType } from "./type";
import { CLOSE_CONSOLE } from "@/service/constant.apis";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import store from "@/store";
import { UPDATE_SCROLL_TOP } from "@/store/constant.mutation.types";
NProgress.configure({ showSpinner: false });

const routes: Array<RouteRecordRaw> = [
  { path: "/", redirect: "/index", name: "/" },
  {
    path: "/index",
    name: "index",
    component: () =>
      import(/* webpackChunkName: "index" */ "@/pages/Index/indexPage.vue"),
  },
  {
    path: "/audit",
    name: "audit",
    component: () =>
      import(/* webpackChunkName: "audit" */ "@/pages/Audit/auditPage.vue"),
  },
  {
    path: "/advantage",
    name: "advantage",
    component: () =>
      import(
        /* webpackChunkName: "advantage" */ "@/pages/Advantage/advantagePage.vue"
      ),
  },
  {
    path: "/about",
    name: "about",
    component: () =>
      import(/* webpackChunkName: "about" */ "@/pages/About/aboutPage.vue"),
  },
  {
    /**当前路径不存在 */
    // path: "/:pathMatch(.*)*",//如果在匹配末尾多加一个*，则会将未知路径解析为一个数组
    path: "/:pathMatch(.*)",
    name: "404",
    meta: {},
    component: () => import("@/pages/Error/page-not-found.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

/**
 * 返回值问题[不推荐使用next()]
 *  1.false ：不进行导航
 *  2.undefined或者不写返回值：进行默认导航
 *  3.字符串：路径，跳转到对应的路径中
 *  4.对象 ：类似于 router.push({path:"/login",query:{a:'a'}})
 *
 *router.replace()//不会在浏览器得历史记录中加,只会替代掉最后一条记录
 */
//路由守卫
router.beforeEach((to: RouterType, from: RouterType) => {
  NProgress.start();
  if (!CLOSE_CONSOLE) {
    console.groupCollapsed(
      `路由守卫log【${String(to?.name)}】-【${String(from?.name)}】`
    );
    console.log(to, from);
    console.groupEnd();
  }

  store.commit(UPDATE_SCROLL_TOP); /**路由切换时，将页面滚动条恢复至顶部 */
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
